package com.dailyblue.java.spring.boot.mvc.mybatis.controller;

import com.dailyblue.java.spring.boot.commons.bean.User;
import com.dailyblue.java.spring.boot.commons.exception.UserCodeNotFoundException;
import com.dailyblue.java.spring.boot.commons.exception.UserPasswordErrorException;
import com.dailyblue.java.spring.boot.commons.util.JsonResult;
import com.dailyblue.java.spring.boot.mvc.mybatis.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public JsonResult login(User user) throws UserCodeNotFoundException, UserPasswordErrorException {
        return userService.login(user);
    }

    @GetMapping("/logout")
    public JsonResult logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        return userService.logout(token);
    }

    @GetMapping("/is")
    public JsonResult isLogin(HttpServletRequest request) {
        return userService.isLogin(request.getHeader("token"));
    }
}
